/* ************************************************************************************************
 * Copyright 2017 Qualcomm Technologies International, Ltd.                                       *
 **************************************************************************************************/

package com.shanling.shanlingcontroller.receiver;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * <p>This class allows reception of information from the system about Bluetooth devices bond state information.</p>
 * <p>This receiver should be used with the following intent filter:
 * {@link BluetoothDevice#ACTION_BOND_STATE_CHANGED ACTION_BOND_STATE_CHANGED} and
 * {@link BluetoothDevice#ACTION_PAIRING_REQUEST ACTION_PAIRING_REQUEST}.</p>
 */
public class BondStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()){
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_NONE:
                        Log.e("weiyu", "取消配对");
                        EventBus.getDefault().post(new EventCenter<>(Constants.IS_BOND, false));
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        Log.e("weiyu", "配对中");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        Log.e("weiyu", "配对成功");
                        EventBus.getDefault().post(new EventCenter<>(Constants.IS_BOND, true));
                        break;
                }
                break;
        }
    }



}
